package view;

import java.awt.Color;
import java.awt.Font;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

import model.Config;
import util.DaoGenerico;
import util.Utilitarios;

public class Splash extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private static final JProgressBar progressBar = new JProgressBar();
	private JLabel lblImagem = new JLabel("");
	private final JLabel lblAutomec = new JLabel("AutoMec 1.0");
	private static Config config;

	public Splash() {
		lblAutomec.setFont(new Font("Tahoma", Font.BOLD, 19));
		initGUI();
		config = Utilitarios.getConfigSistema();
	}

	private void initGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(442, 300);
		setLocationRelativeTo(null);
		lblImagem.setIcon(new ImageIcon(Splash.class.getResource("/imagens/img_splash.jpg")));
		
		lblImagem.setHorizontalAlignment(SwingConstants.CENTER);

		progressBar.setStringPainted(true);
		progressBar.setForeground(new Color(127, 255, 0));		
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		setContentPane(contentPane);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addComponent(lblImagem, GroupLayout.PREFERRED_SIZE, 424, Short.MAX_VALUE)
				.addComponent(progressBar, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(144)
					.addComponent(lblAutomec, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(153, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(lblAutomec, GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblImagem, GroupLayout.PREFERRED_SIZE, 183, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(progressBar, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE))
		);
		contentPane.setLayout(gl_contentPane);
	}

	private static void carregar(){
		for (int i = 0; i < 101; i++) {
			try {
				Thread.sleep(80);

				progressBar.setValue(i);

			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		/*try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(MenuPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(MenuPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(MenuPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(MenuPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}*/

		Splash frame = new Splash();

		frame.setUndecorated(true);
		frame.setResizable(false);
		frame.setVisible(true);  

		new Thread() {
			public void run() {
				carregar();				
			};

		}.start();

		DaoGenerico.getEntityManager();
		
		if ((config != null) && (config.isUsaControleAcesso())){ 
			AcessoSistema as = new AcessoSistema(null);
			as.setUndecorated(true);
			as.setVisible(true);
		} else {
			new MenuPrincipal(null).setVisible(true);
		}
		
		frame.dispose();
	}
}
